<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>选择资金渠道及金额</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>资金渠道明细</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th style="width: 35px!important;padding-left: 0"><input type="checkbox" name="xuanAll" id="xuanAll" lay-filter="xuanAll" lay-skin="primary"></th>
                            <th>资金渠道</th>
                            <th>可使用金额(元)</th>
                            <th><text>*</text>本次使用金额(元)</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div>合计：<span id="allmoney">0</span> 元</div>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var parent_domkey = getQueryVariable('domkey');
    var parent_enginid = getQueryVariable('enginid') || 0;



    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var quText = $("#list_list tr", parent.document).eq(parent_domkey).find('.quobj').text();
        if(quText){
            var quobj = JSON.parse(quText);
        }else{
            var quobj = {};
        }


        function check_exist(data, type, qienid) {
            var money = '';
            var res = false;
            for (var key in data){
                if(data[key].type == type){
                    if(type == 3){
                        if(data[key].qienid == qienid){
                            money = data[key].money
                            res = true;
                        }
                    }else{
                        money = data[key].money
                        res = true;
                    }
                }
            }
            return {money:money, res:res};
        }


        var existArr = [];
        $('#list_list tr', parent.document).each(function (i) {
            var quobj = $(this).find('.quobj').text();
            var quArr = [];
            if(quobj){
                quArr = JSON.parse(quobj);
            }
            for (var mmm in quArr){
                existArr.push(quArr[mmm]);
            }
        });


        facade.ajax({url: ask_gongzhi_qu_list, data:{enginid:parent_enginid}}).done(function(res){
            if (res.code === 2000) {
                var list = res.data.list;

                if(list.length > 0){
                    for (var key in list){
                        var zongMoney = parseFloat(list[key].banlance);
                        for (var nnn in existArr){
                            if(existArr[nnn].type == list[key].type && existArr[nnn].qienid == list[key].qienid){
                                zongMoney = parseFloat(zongMoney) - parseFloat(existArr[nnn].money);
                            }
                        }
                        list[key].banlance = parseFloat(zongMoney).toFixed(2);


                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';

                        var existInfo = check_exist(quobj, list[key].type, list[key].qienid);
                        if(existInfo.res){
                            str += '<td style="width: 45px!important;"><input type="checkbox" name="xuanhang" lay-filter="xuanhang" class="xuanhang" checked lay-skin="primary"></td>';
                        }else{
                            str += '<td style="width: 45px!important;"><input type="checkbox" name="xuanhang" lay-filter="xuanhang" class="xuanhang" lay-skin="primary"></td>';
                        }
                        str += '<td><input type="text" value="'+list[key].name+'" datatype="'+list[key].type+'" qienid="'+list[key].qienid+'" class="layui-input disabled name" disabled></td>';
                        str += '<td><input type="number" value="'+list[key].banlance+'" class="layui-input disabled banlance" disabled></td>';
                        if(existInfo.res){
                            str += '<td><input type="number" value="'+existInfo.money+'" class="layui-input money"></td>';
                        }else{
                            str += '<td><input type="number" value="" class="layui-input money"></td>';
                        }
                        str += '</tr>';
                        $("#list_list").append(str);
                        update_refresh_row_func()
                    }
                }

                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('checkbox(xuanAll)', function(data){
            if(this.checked){
                $('.xuanhang').prop("checked", true);
            }else{
                $(".xuanhang").prop('checked', false);
            }
            render_from();
        });
        form.on('checkbox(xuanhang)', function(data){
            jisuan_allmoney_all();
        });


        form.on('submit(add)', function(data){
            var list = [];
            var allmoney = 0;
            try {
                $('#list_list tr').each(function (i) {
                    var checked = $(this).find('.xuanhang').prop("checked");
                    if(checked){
                        var type = $(this).find('.name').attr('datatype');
                        var qienid = $(this).find('.name').attr('qienid') || 0;
                        var banlance = $(this).find('.banlance').val();
                        var money = $(this).find('.money').val() || 0;

                        var index = i + 1;
                        if(!money){
                            throw new Error('第'+index+'行，请输入本次使用金额');
                        }

                        if(parseFloat(money) > parseFloat(banlance)){
                            throw new Error('第'+index+'行，本次使用金额已大于余额');
                        }
                        allmoney = parseFloat(parseFloat(allmoney) + parseFloat(money)).toFixed(2);
                        list.push({
                            type:type,
                            qienid:qienid,
                            money:money,
                        });
                    }
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();



            $("#list_list tr", parent.document).eq(parent_domkey).find('.money').val(allmoney);
            $("#list_list tr", parent.document).eq(parent_domkey).find('.quobj').html(JSON.stringify(list));
            parent.update_refresh_row_func()
            layer_close();
            return false;
        });

    });




    $('#list_list').on('input propertychange','.money',function () {
        jisuan_allmoney_all();
    });

    function jisuan_allmoney_all() {
        var allmoney = 0;
        $('#list_list tr').each(function (i) {
            var checked = $(this).find('.xuanhang').prop("checked");
            if(checked){
                var money = parseValidNum($(this).find('.money').val());
                allmoney = parseFloat(allmoney) + parseFloat(money);
            }
        });
        $("#allmoney").text(parseFormatNum(allmoney,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_allmoney_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }
</script>
</body>
</html>
